/*
 * ==================================================
 * 项目开发者：JerryStark
 * 开发者Email：4771007@qq.com
 * 开发者QQ:4771007
 * 开发范围：web，wap，android,ios,osx or win or linux 应用
 * ==================================================
 */
package RTGame.Panels;

import RTGame.Configs.Load;
import RTPower.RTHttp;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 获取后台服务器信息类
 * <br>独立线程定时获取服务器信息
 * <br>发起指令
 *
 * @author jerry
 */
public class GetServerData {

    /**
     * 控制中心通讯线程
     */
    public Thread ConterThread;

    /**
     * 状态获取,单一顺序的线程池
     * <br>排队执行
     */
    public static ExecutorService GetServerExecutor = Executors.newSingleThreadExecutor();

    /**
     * 抓取的服务器状态json对象
     * <br>抓取时候字符串转译过来的
     *
     */
    public JsonObject GetServerData;

    /**
     * 初始化加载使用的配置
     */
    public GetServerData() {

    }

    /**
     * 开启控制中心线程
     */
    public void Start() {
        ConterThread = new Thread(() -> {
            //循环获取后台服务器信息
            while (true) {
                GetServerStart();
                try {
                    //1秒获取一次服务器的状态
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(GetServerData.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        ConterThread.start();
    }

    /**
     * 获取服务器状态
     */
    private void GetServerStart() {

        GetServerExecutor.execute(() -> {

            String set_url = "http://127.0.0.1:" + Load.MainConfig.get("ServerPort").getAsInt() + "/?action=GetServerStatus";
            RTHttp rthttp = new RTHttp(set_url);
            String result = rthttp.Get();
            System.out.println(result);
            Gson gson = new Gson();
            JsonObject json = gson.fromJson(result, JsonObject.class);

            //如果状态不是ok 就终止
            if (!json.get("status").getAsString().equals("ok")) {
                return;
            }
            GetServerData = json.getAsJsonObject("data");
            rthttp.close();

            //抓取完毕激活显示刷新
            REShowPanel();
        });
    }

    /**
     * 刷新界面显示
     */
    private void REShowPanel() {
 

    }
}
